package com.gz.logistics.library.listener;

import com.gz.logistics.library.util.Constants;
import com.gz.logistics.library.util.CrashHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 批量签收“物流” 或者“业务”
 * author wanner1991
 */
public abstract class BatchOrdersStatusSignListener extends BaseListener{
    private String TAG = BatchOrdersStatusSignListener.class.getSimpleName();
    @Override
    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
        try {
            int status = response.getInt(Constants.REQ_STATUS);
            String msg = response.getString(Constants.REQ_MSG);

            switch(status){
                case Constants.REQ_OK:
                    onResponseSuccess();
                    break;

                case Constants.REQ_USEID_IS_EMPTY: // 用户ID为空
                case Constants.REQ_SN_IS_EMPTY:    // 订单编号为空
                case Constants.REQ_NO_SPECIFIC_THIS_ORDER: // 该订单没有分配给该用户
                    onDataError();
                    break;
                case Constants.REQ_PART_ORDERS_SIGN_FAIL:
                    onPartOrdersFail();
                    break;
                case Constants.REQ_EXIST_ORDERS_THAT_HAS_NOST_SIGN_LOGISTICS:
                    // 批量业务签收时，如果存在还未进行物流签收的订单，那么服务器会把 还未进行物流确认的订单编号，返回给app
                    // sn 之间以逗号隔开
                    onExistOrdersHasNotSignLogistics();
                    break;
                default:
                    onResponseFailed();


            }

        } catch (JSONException e) {
            onFailure(statusCode, headers, e, new JSONObject());
        }
    }
    public abstract void onResponseSuccess();

    /**
     * 在批量进行业务签收时，存在某些订单还未进行物流签收
     */
    public abstract void onExistOrdersHasNotSignLogistics();

    /**
     *部分订单签收失败
     */
    public abstract void onPartOrdersFail();


    /**
     * 用户ID为空
     * 订单编号为空
     * 该订单没有分配给该用户
     */
    public abstract void onDataError();
    @Override
    public void onNoMoreData() {

    }

    @Override
    public void onOffline() {

    }

    @Override
    public void onFailure(int statusCode,
                          Header[] headers, Throwable throwable,
                          JSONObject errorResponse) {
        onResponseFailed();
        CrashHandler.e(throwable);
    }


}
